package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FastDFSUtil;
import com.ruoyi.system.domain.SysPrizeManagement;
import com.ruoyi.system.mongo.SysUserDao;
import com.ruoyi.system.service.ISysPrizeEventService;
import com.ruoyi.system.service.ISysPrizeManagementService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author HP
 * @Date 2021/2/24
 * @Description TODO
 */
@Api("礼品管理")
@RestController
@RequestMapping("/system/gift")
@Slf4j
public class SysPrizeManagementController {
    @Autowired
    private ISysPrizeManagementService sysPrizeManagementService;
    @Autowired
    private ISysPrizeEventService iSysPrizeEventService;
    @Autowired
    private FastDFSUtil fastDFSUtil;
    /*
    查询所有礼品信息
     */
    @GetMapping("/selectSysPrizeManagement")
    public AjaxResult selectSysPrizeManagement(SysPrizeManagement sysPrizeManagement,Integer pageNum,Integer pageSize){
        List<SysPrizeManagement> sysPrizeManagements=sysPrizeManagementService.selectSysPrizeManagement(sysPrizeManagement,pageNum,pageSize);
        PageInfo pageInfo = new PageInfo(sysPrizeManagements);
        return AjaxResult.success(pageInfo);
    }

    /*
    添加礼品信息
     */
    @PostMapping("/saveSysPrizeManagement")
    public AjaxResult saveSysPrizeManagement(@RequestParam String prizename,
                                             @RequestParam(required = false) String describes,
                                             @RequestParam int oldintegral,
                                             @RequestParam int nowintegral,
                                             @RequestParam int counts,
                                             @RequestParam String operator,
                                             @RequestParam MultipartFile eppic)
    {
        String fileFullPath=null;
        try {
            //文件上传到FastDFS文件服务器上
            fileFullPath = fastDFSUtil.uploadFile(eppic);  //upload to fastdfs
        } catch (Exception e) {
            log.error("upload file Exception!",e);
        }
        if (fileFullPath==null) {
            log.error("upload file failed,please upload again!");
        }
        //获得FastDFS完整的URL地址
        String urlPath=fastDFSUtil.getResAccessUrl(fileFullPath);
        //封装
        SysPrizeManagement sysPrizeManagement=new SysPrizeManagement();
        sysPrizeManagement.setPrizename(prizename);
        sysPrizeManagement.setDescribes(describes);
        sysPrizeManagement.setOperator(operator);
        sysPrizeManagement.setOldintegral(oldintegral);
        sysPrizeManagement.setNowintegral(nowintegral);
        sysPrizeManagement.setCounts(counts);
        sysPrizeManagement.setEppic(urlPath);
        sysPrizeManagementService.saveSysPrizeManagement(sysPrizeManagement);
        return AjaxResult.success();
    }
    /*
    删除礼品信息
     */
    @DeleteMapping("/deleteSysPrizeManagement/{pid}")
    public AjaxResult deleteSysPrizeManagement(@PathVariable int pid){
        sysPrizeManagementService.deleteSysPrizeManagement(pid);
        iSysPrizeEventService.deletePid(pid);
        return AjaxResult.success();
    }
    /*
    根据id查询
     */
    @GetMapping("/selectById")
    public AjaxResult selectById(int pid){
        SysPrizeManagement sysPrizeManagement = sysPrizeManagementService.selectById(pid);
        return AjaxResult.success(sysPrizeManagement);
    }
    /*
    修改礼品信息
     */
    @PutMapping("/updateSysPrizeManagement")
    public AjaxResult saveSysPrizeManagement(@RequestParam int pid,
                                             @RequestParam String prizename,
                                             @RequestParam(required = false) String describes,
                                             @RequestParam int oldintegral,
                                             @RequestParam int nowintegral,
                                             @RequestParam int counts,
                                             @RequestParam String operator,
                                             @RequestParam MultipartFile eppic)
    {
        String fileFullPath=null;
        String urlPath = null;
        String filename = eppic.getOriginalFilename();
        try {
//            System.out.printf(eppic);
//           我添加的修改图片,如果是原图就不修改,不是原图就修改
            //文件上传到FastDFS文件服务器上
            if(!filename.isEmpty()){
                fileFullPath = fastDFSUtil.uploadFile(eppic);  //upload to fastdfs
                //获得FastDFS完整的URL地址
                urlPath=fastDFSUtil.getResAccessUrl(fileFullPath);
            }
        } catch (Exception e) {
            log.error("upload file Exception!",e);
        }
        //这个是注释了你的代码 觉得这个不需要
//        if (fileFullPath==null) {
//            log.error("upload file failed,please upload again!");
//        }

        //封装
        SysPrizeManagement sysPrizeManagement=new SysPrizeManagement();
        sysPrizeManagement.setPrizename(prizename);
        sysPrizeManagement.setDescribes(describes);
        sysPrizeManagement.setOldintegral(oldintegral);
        sysPrizeManagement.setNowintegral(nowintegral);
        sysPrizeManagement.setCounts(counts);
        sysPrizeManagement.setOperator(operator);
        if(!filename.isEmpty()){
            sysPrizeManagement.setEppic(urlPath);
        }
        sysPrizeManagement.setPid(pid);
        sysPrizeManagementService.updateSysPrizeManagement(sysPrizeManagement);
        return AjaxResult.success();
    }
    /*
    修改获奖礼品数量
     */
    @PutMapping("/updateByCounts")
    public AjaxResult updateByCounts(SysPrizeManagement sysPrizeManagement){
        int pid = sysPrizeManagement.getPid();
        SysPrizeManagement oldSysPrizeManagement1 = sysPrizeManagementService.selectById(pid);
        //总数量
        int counts1 = oldSysPrizeManagement1.getCounts();
        //奖品数量
        int counts = sysPrizeManagement.getCounts();
        if (counts1-counts<0){
            return AjaxResult.error();
        }else{
            int a=counts1-counts;
            sysPrizeManagement.setCounts(a);
            sysPrizeManagement.setPid(pid);
            sysPrizeManagementService.updateByCounts(sysPrizeManagement);
            return AjaxResult.success();
        }
    }
    /*
    修改获奖礼品数量
     */
    @PutMapping("/saveByCounts")
    public AjaxResult saveByCounts(SysPrizeManagement sysPrizeManagement){
        sysPrizeManagementService.saveByCounts(sysPrizeManagement);
        return AjaxResult.success();
    }

    /*
    修改获奖礼品数量
     */
    @PutMapping("/saveByCounts1")
    public AjaxResult saveByCounts1(SysPrizeManagement sysPrizeManagement) {
        int pid = sysPrizeManagement.getPid();
        SysPrizeManagement oldSysPrizeManagement1 = sysPrizeManagementService.selectById(pid);
        //总数量
        int countss = oldSysPrizeManagement1.getCounts();
        //修改后数量
        int counts = sysPrizeManagement.getCounts();
        //修改前奖品数量
        int counts1 = sysPrizeManagement.getCounts1();
        int count = countss + counts1 - counts;
        if (count < 0) {
            return AjaxResult.error();
        } else {
            sysPrizeManagement.setCounts(count);
            sysPrizeManagementService.saveByCounts1(sysPrizeManagement);
            return AjaxResult.success();
        }


    }
}
